package com.nian.trancation.tc.a.service;

import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.codingapi.txlcn.tc.annotation.TccTransaction;
import com.nian.trancation.tc.a.dao.TableTestARepsitory;
import com.nian.trancation.tc.a.model.TableTestA;
import com.nian.trancation.tc.b.api.TableTestBAPI;
import com.nian.trancation.tc.b.model.TableTestB;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/15 0015 11:52
 */
@Service
public class TableTestAService {

    @Autowired
    private TableTestARepsitory tableTestARepsitory;

    @Autowired
    private TableTestBAPI tableTestBAPI;

    @LcnTransaction //分布式事务注解
    @Transactional  //本地事务注解
    public TableTestA save(TableTestA model){
        Date date = new Date();
        if(model.getId() !=null){
            model.setUpdateTime(date);
        }else {
            model.setCreateTime(date);
            model.setUpdateTime(date);
        }
        TableTestA info = tableTestARepsitory.save(model);
        // 调用B服务
        TableTestB modelB = new TableTestB();
        BeanUtils.copyProperties(model, modelB);
        tableTestBAPI.save(modelB);
        return info;
    }

    @LcnTransaction //分布式事务注解
    @Transactional  //本地事务注解
    public void delete(TableTestA model){
        tableTestARepsitory.delete(model);
    }

}
